package segimasat;


import segimasat.gui.MainFrame;
import ij.*;
import ij.plugin.filter.PlugInFilter;
import ij.process.ImageProcessor;
import segimasat.image.Image;


public class __Main implements PlugInFilter{
    Image f;

    public int setup(String string, ImagePlus impl){  
        f = getInputImage(impl);
        if (f == null)
            return DONE;
        return /*DOES_STACKS+*/DOES_RGB+DOES_8G+DOES_32+NO_CHANGES;
    }

    public void run(ImageProcessor ip){
        MainFrame frame = new MainFrame(f);
    }

    public static Image getInputImage(ImagePlus imp){
        if (imp == null){
            IJ.error("ERROR", "The are no images open.");
            return null;
        }
        if (!imp.isProcessor()){
            IJ.error("ERROR", "The ImagePlus opened is not valid.");
            return null;
        }
        if (imp.getStackSize() == 1){
            if (imp.getType() == ImagePlus.COLOR_RGB) return new segimasat.image.integer.RGB(imp);
            else if (imp.getType() == ImagePlus.GRAY8) return new segimasat.image.integer.Gray8(imp);
            else if (imp.getType() == ImagePlus.GRAY32) return new segimasat.image.floating.Gray32(imp);
            IJ.error("ERROR", "The type image must be RGB or 8 bits gray or 32 bits gray.");
            return null;
        }
        if (imp.getStackSize() > 1){
            if (imp.getType() == ImagePlus.GRAY8) return new segimasat.image.integer.StackGray8(imp);
            if (imp.getType() == ImagePlus.GRAY32) return new segimasat.image.floating.StackGray32(imp);
            //else if (imp.getType() == ImagePlus.COLOR_RGB) return new StackRGB(imp);
            //IJ.error("ERROR", "The stack must be 32 bits floating point or 8 bits.");
            //IJ.error("ERROR", "The stack must be 8 bits or RGB type.");
            IJ.error("ERROR", "The stack type must be 8 bits gray or 32 bits gray.");
            return null;
        }
        IJ.error("ERROR", "Unknown error.");
        return null;
    }
}